1
|
|
|
var Conclusion = require('./Conclusion'), |
2
|
|
|
NameForm = require('./NameForm'), |
3
|
|
|
GDate = require('./Date'), |
4
|
|
|
utils = require('./utils'); |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* A name. |
8
|
|
|
* |
9
|
|
|
* @constructor |
10
|
|
|
* @param {Object} [json] |
|
|
|
|
11
|
|
|
*/ |
12
|
|
|
var Name = function(json){ |
13
|
|
|
|
14
|
|
|
// Protect against forgetting the new keyword when calling the constructor |
15
|
|
|
if(!(this instanceof Name)){ |
16
|
|
|
return new Name(json); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
// If the given object is already an instance then just return it. DON'T copy it. |
20
|
|
|
if(Name.isInstance(json)){ |
21
|
|
|
return json; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
Conclusion.call(this, json); |
25
|
|
|
|
26
|
|
|
if(json){ |
|
|
|
|
27
|
|
|
this.setType(json.type); |
28
|
|
|
this.setDate(json.date); |
29
|
|
|
this.setNameForms(json.nameForms); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
}; |
32
|
|
|
|
33
|
|
|
Name.prototype = Object.create(Conclusion.prototype); |
34
|
|
|
|
35
|
|
|
Name._gedxClass = Name.prototype._gedxClass = 'GedcomX.Name'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Check whether the given object is an instance of this class. |
39
|
|
|
* |
40
|
|
|
* @param {Object} obj |
41
|
|
|
* @returns {Boolean} |
42
|
|
|
*/ |
43
|
|
|
Name.isInstance = function(obj){ |
44
|
|
|
return utils.isInstance(obj, this._gedxClass); |
45
|
|
|
}; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get the name type |
49
|
|
|
* |
50
|
|
|
* @returns {String} type |
51
|
|
|
*/ |
52
|
|
|
Name.prototype.getType = function(){ |
53
|
|
|
return this.type; |
54
|
|
|
}; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Set the name type |
58
|
|
|
* |
59
|
|
|
* @param {String} type |
60
|
|
|
* @returns {Name} This instance |
61
|
|
|
*/ |
62
|
|
|
Name.prototype.setType = function(type){ |
63
|
|
|
this.type = type; |
64
|
|
|
return this; |
65
|
|
|
}; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the date |
69
|
|
|
* |
70
|
|
|
* @returns {Date} date |
71
|
|
|
*/ |
72
|
|
|
Name.prototype.getDate = function(){ |
73
|
|
|
return this.date; |
74
|
|
|
}; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set the date |
78
|
|
|
* |
79
|
|
|
* @param {Date|Object} date |
80
|
|
|
* @returns {Fact} This instance |
81
|
|
|
*/ |
82
|
|
|
Name.prototype.setDate = function(date){ |
83
|
|
|
if(date){ |
84
|
|
|
this.date = GDate(date); |
85
|
|
|
} |
86
|
|
|
return this; |
87
|
|
|
}; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get the name forms |
91
|
|
|
* |
92
|
|
|
* @return {NameForm[]} |
93
|
|
|
*/ |
94
|
|
|
Name.prototype.getNameForms = function(){ |
95
|
|
|
return this.nameForms || []; |
96
|
|
|
}; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Set the name forms |
100
|
|
|
* |
101
|
|
|
* @param {NameForm[]|Object[]} nameForms |
102
|
|
|
* @returns {Name} This instance |
103
|
|
|
*/ |
104
|
|
|
Name.prototype.setNameForms = function(nameForms){ |
105
|
|
|
return this._setArray(nameForms, 'nameForms', 'addNameForm'); |
106
|
|
|
}; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Add a name form |
110
|
|
|
* |
111
|
|
|
* @param {NameForm|Object} nameForm |
112
|
|
|
* @returns {Name} This instance |
113
|
|
|
*/ |
114
|
|
|
Name.prototype.addNameForm = function(nameForm){ |
115
|
|
|
return this._arrayPush(nameForm, 'nameForms', NameForm); |
116
|
|
|
}; |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Export the object as JSON |
120
|
|
|
* |
121
|
|
|
* @return {Object} JSON object |
122
|
|
|
*/ |
123
|
|
|
Name.prototype.toJSON = function(){ |
124
|
|
|
return this._toJSON(Conclusion, [ |
125
|
|
|
'type', |
126
|
|
|
'date', |
127
|
|
|
'nameForms' |
128
|
|
|
]); |
129
|
|
|
}; |
130
|
|
|
|
131
|
|
|
module.exports = Name; |